import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = '/sales/api/v1';

const getInitdesc = formatMessage({
    id: 'api.message.getInitdesc',
    defaultMessage: '初始化'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询生产工厂详情'
});
const getMaintainDetailDesc = formatMessage({
    id: 'api.message.getMaintainDetailDesc',
    defaultMessage: '查询生产工厂与车型关系详情'
});
const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增生产工厂'
});
const modifyDesc = formatMessage({
    id: 'api.message.modifyDesc',
    defaultMessage: '修改生产工厂与车型关系'
});
const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废'
});
const maintainDesc = formatMessage({
    id: 'api.message.maintainDesc',
    defaultMessage: '关系维护'
});
const productCategorysQueryDesc = formatMessage({
    id: 'api.message.productCategorysQueryDesc',
    defaultMessage: '查询产品分类'
});

//初始化
export const getInitData = () => request(`${baseUrl}/ui/manucturer/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitdesc));
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/manucturers?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询生产工厂详情
export const getDetail = id => request(`${baseUrl}/manucturers/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
// 查询生产工厂与车型关系详情
export const getMaintainDetail = id => request(`${baseUrl}/manucturers/${id}/forMaintain`, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getMaintainDetailDesc));
//新增
export const addData = data => request(`${baseUrl}/manucturers`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));
//修改
export const modifyOrder = (id, data) => request(`${baseUrl}/manucturers/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyDesc));
//关系维护
export const maintainData = (id, data) => request(`${baseUrl}/manucturers/${id}/maintain`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, maintainDesc));
// 作废
export const abandonData = (id, data) => request(`${baseUrl}/manucturers/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));
// 查询产品分类
export const productCategorysQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/productCategories/forManucturerProductCategory?${queryString}`, null, dto.ProductCategorys)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, productCategorysQueryDesc));
};


